namespace WLFramework.Validation
{
    using System;
    
	[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
	public class MaxDateAttribute : MaxValueAttribute
	{
        public MaxDateAttribute(int year, int month, int day)
            : this(year, month, day, null, null)
        { 
        }

		public MaxDateAttribute(int year, int month, int day, string errorMessage)
			: this(year, month, day, errorMessage, null)
		{
		}

        public MaxDateAttribute(int year, int month, int day, string errorMessage, string helpMessage)
            : base(new DateTime(year, month, day), errorMessage, helpMessage)
        { 
        }

        public override ValidateResult IsValid(ValidationContext context)
        {
            if (context.IsNull(context.Value))
                return ValidateResult.Success;

            DateTime contextValue = Convert.ToDateTime(context.Value);

            if (contextValue > (DateTime)this.Value)
            {
                return new ValidateResult(
                    string.Format(this.ErrorMessage, context.DisplayName, this.Value));
            }

            return ValidateResult.Success;
        }
	}
}
